from django.contrib import admin

from apps.area.models import Area
from apps.products.models import Category, Product, ProductFile


class ProductFileInline(admin.TabularInline):
    model = ProductFile
    extra = 1  # 默认显示一个额外的空行，用于添加新记录


class ProductInline(admin.TabularInline):
    model = Product
    extra = 0  # 默认显示一个额外的空行，用于添加新记录


class AreaInline(admin.TabularInline):
    model = Area
    extra = 0  # 默认显示一个额外的空行，用于添加新记录


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'title', 'parent', 'description', 'modified_at', 'created_at')
    search_fields = ('name', 'id')
    list_filter = ('name', 'title', 'description')
    ordering = ('name',)
    inlines = [ProductInline, AreaInline]  # 允许在目录管理页面中直接管理产品、应用领域


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ('category', 'name', 'brief_description', 'detailed_description', 'created_at')
    search_fields = ('name', 'category')
    list_filter = ('created_at',)
    ordering = ['-created_at']
    inlines = [ProductFileInline]  # 允许在产品管理页面中直接管理产品文件


@admin.register(ProductFile)
class ProductFileAdmin(admin.ModelAdmin):
    list_display = ('product', 'name', 'download_type', 'file', 'created_at')
    search_fields = ('name', 'product__name', 'download_type')
    list_filter = ('download_type', 'product')
    ordering = ['product_id']
